package com.eduplatform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eduplatform.common.Result;
import com.eduplatform.common.UserContext;
import com.eduplatform.dto.AIMaterialGenerateDTO;
import com.eduplatform.dto.MaterialDTO;
import com.eduplatform.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 课件笔记控制器
 */
@RestController
@RequestMapping("/api/materials")
@CrossOrigin
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private UserContext userContext;

    /**
     * 分页获取课件笔记列表
     */
    @GetMapping("/list")
    public Result<Page<MaterialDTO>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer educationLevel,
            @RequestParam(required = false) Long subjectId,
            @RequestParam(required = false) Long schoolId) {
        Page<MaterialDTO> result = materialService.listMaterials(page, size, keyword, type, educationLevel, subjectId,
                schoolId);
        return Result.success(result);
    }

    /**
     * 获取课件笔记详情
     */
    @GetMapping("/detail/{id}")
    public Result<MaterialDTO> detail(@PathVariable Long id) {
        MaterialDTO materialDTO = materialService.getMaterialById(id);
        if (materialDTO == null) {
            return Result.error("课件笔记不存在");
        }
        return Result.success(materialDTO);
    }

    /**
     * 添加课件笔记
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody @Valid MaterialDTO materialDTO) {
        boolean result = materialService.addMaterial(materialDTO);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 更新课件笔记
     */
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody @Valid MaterialDTO materialDTO) {
        boolean result = materialService.updateMaterial(materialDTO);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除课件笔记
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = materialService.deleteMaterial(id);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 使用AI生成课件/笔记
     */
    @PostMapping("/ai-generate")
    public Result<MaterialDTO> generateAIMaterial(@RequestBody @Valid AIMaterialGenerateDTO generateDTO) {
        Long currentUserId = userContext.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }

        MaterialDTO materialDTO = materialService.generateAIMaterial(generateDTO, currentUserId);
        return Result.success(materialDTO);
    }
}